#include "server.h"

void login(char* buf)
{
	char path[PATH_LEN] = {};
	sprintf(path,"%s%s",ACC_PATH,ctos.acc.bank);

	//判断银行卡号是否正确
	if(0 != access(path,F_OK))
	{
		sprintf(buf,"N:卡号不存在，请检查!");
		return;
	}

	int fd = open(path,O_RDWR);
	if(0 > fd)
	{
		error("open");
		sprintf(buf,"N:服务器正在升级，登录失败!");
		return;
	}

	Acc acc = {};
	read(fd,&acc,sizeof(Acc));

	if(acc.isLock >= 3)
	{
		sprintf(buf,"N:此账号已被锁定，请解锁!");
		return;
	}

	if(strcmp(ctos.acc.password,acc.password))
	{
		acc.isLock++;
		sprintf(buf,"N:密码错误，重新登录(您还有%d次机会)!",3-acc.isLock);
		if(acc.isLock >= 3)
		{	
			sprintf(buf,"N:此账户已被锁定，请解锁!");
		}
	}
	else
	{
		acc.isLock = 0;
		sprintf(buf,"Y:恭喜您登录成功!");
	}

	lseek(fd,0,SEEK_SET);
	write(fd,&acc,sizeof(Acc));
	close(fd);
}